import 'dart:convert' show json;

import 'HomeData.dart';

T asT<T>(dynamic value) {
  if (value is T) {
    return value;
  }

  return null;
}

class SearchData {
  SearchData({
    this.data,
    this.qty,
    this.status,
  });

  factory SearchData.fromJson(Map<String, dynamic> jsonRes) {
    if (jsonRes == null) {
      return null;
    }

    final List<HomeDataItem> data = jsonRes['data'] is List ? <HomeDataItem>[] : null;
    if (data != null) {
      for (final dynamic item in jsonRes['data']) {
        if (item != null) {
          data.add(HomeDataItem.fromJson(asT<Map<String, dynamic>>(item)));
        }
      }
    }
    return SearchData(
      data: data,
      qty: asT<int>(jsonRes['qty']),
      status: asT<int>(jsonRes['status']),
    );
  }

  List<HomeDataItem> data;
  int qty;
  int status;

  Map<String, dynamic> toJson() => <String, dynamic>{
        'data': data,
        'qty': qty,
        'status': status,
      };
  @override
  String toString() {
    return json.encode(this);
  }
}
